package com.jd.pet.service.impl;

import com.jd.basic.utils.*;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.domain.SearchMasterMsgAuditLog;
import com.jd.pet.dto.AcceptDto;
import com.jd.pet.dto.SearchMasterMsgDto;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.pet.query.SearchMasterMsgQuery;
import com.jd.pet.service.ISearchMasterMsgService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.user.domain.Logininfo;
import com.jd.user.domain.User;
import com.jd.user.mapper.LogininfoMapper;
import com.jd.user.mapper.UserMapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.poi.poifs.crypt.dsig.OOXMLURIDereferencer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2023-03-16
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    @Transactional
    public void publish(SearchMasterMsgDto dto) {
        // 校验参数（已完成）
        // 寻主信息存入数据库
        SearchMasterMsg msg = new SearchMasterMsg();
        BeanUtils.copyProperties(dto,msg);
        // 获取当前用户信息
        Logininfo loginInfo = LoginContext.getLoginInfo();
        User user = userMapper.findBylogininfoId(loginInfo.getId());
        msg.setUserId(user.getId());
        msg.setState(0);
        searchMasterMsgMapper.add(msg);
        // 百度ai校验
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            // 校验不通过，需要保存寻主消息审核：t_search_master_msg_audit_log
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setAuditTime(new Date());
            log.setAuditId(-1L);
            log.setMsgId(msg.getId());
            log.setState(0);
            log.setNote("宠物名称违规:" + dto.getName());
            searchMasterMsgAuditLogMapper.add(log);
            msg.setState(2);
        }else {
            // 人工校验
            // 系统分配店铺接收
            // 获取所有的店铺
            List<Shop> shops = shopMapper.findAll();
            Point address = DistanceUtil.getPoint(dto.getAddress());
            Shop nearestShop = DistanceUtil.getNearestShop(address, shops);
            msg.setShopId(nearestShop.getId());
            msg.setState(1);
        }
        searchMasterMsgMapper.update(msg);
        // todo 生成收购单
    }

    @Override
    @Transactional
    public void accept(AcceptDto dto) {
        // 校验参数
        // 通过msgId获取msg所有的信息
        SearchMasterMsg msg = searchMasterMsgMapper.findById(dto.getMsgId());
        // 生成宠物信息
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setShopId(msg.getShopId());
        pet.setCostprice(BigDecimal.valueOf(msg.getPrice()));
        pet.setState(0);
        pet.setCreatetime(new Date());
        pet.setTypeId((long)(msg.getPetType()));
        pet.setSearchMasterMsgId(msg.getId());
        petMapper.add(pet);
        // 生成收购单
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setAddress(msg.getAddress());
        order.setShopId(msg.getShopId());
        order.setDigest(dto.getNote());
        order.setEmpId(dto.getHandle());
        order.setLastConfirmTime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        order.setMsgId(msg.getId());
        order.setPetId(pet.getId());
        order.setPrice(BigDecimal.valueOf(msg.getPrice()));
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(msg.getUserId()));
        order.setPayType(1);
        order.setState(0);
        order.setUserId(msg.getUserId());
        orderPetAcquisitionMapper.add(order);
        // 修改寻主信息状态
        msg.setState(3);
        searchMasterMsgMapper.update(msg);
    }

    @Override
    public void reject(Long msgId) {
        // 校验参数
        if (null == msgId){
            throw new BusinessException("系统错误");
        }
        // 清楚寻主消息里的shopId
        SearchMasterMsg msg = searchMasterMsgMapper.findById(msgId);
        msg.setShopId(null);
        searchMasterMsgMapper.updateByMsgId(msg);
        // 记录拒单
    }

}
